Imports CapaDeNegocios
Partial Public Class FrmConceptosABM

    Inherits System.Web.UI.Page
    Dim Concepto As CapaDeNegocios.nConcepto = New CapaDeNegocios.nConcepto()
    Dim imputacion As CapaDeNegocios.nImputacion = New CapaDeNegocios.nImputacion

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Session("Login") = "Anonimo" Then
            Response.Redirect("/")
        End If
        If Not Page.IsPostBack Then

            ProcLlenarGrid()
            LlenarComboIimputacion()
            LblError.Visible = False
        End If
    End Sub

    Protected Sub LinkGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkGuardar.Click
        Dim Cont As Integer
        Cont = 0
        LblError.Visible = False
        LblError.Text = ""
        If Len(TxtDescripcion.Text) = 1 Then
            LblError.Text = "Error, la descripcion no es correcta"
            Cont = Cont + 1
        End If
        If TxtIva.Text = "" Then
            LblError.Text = "Error, el campo iva debe estar completo"
            Cont = Cont + 1
        End If
        Dim iva As Double
        If TxtIva.Text <> "" Then
            iva = CDbl(TxtIva.Text)
        Else
            iva = 0
        End If
        If CboTipo.SelectedValue.ToString() = "Ninguno" Then
            LblError.Text = "Error, debe seleccionar un tipo"
            Cont = Cont + 1
        End If
        If CboImputacion.SelectedValue.ToString() = "Ninguno" Then
            LblError.Text = "Error, debe seleccionar una imputacion"
            Cont = Cont + 1
        End If
        If HFCodigoConcepto.Value = "" Then
            If Cont = 0 Then
                If Not Concepto.Existe(Trim(TxtDescripcion.Text), CboTipo.SelectedValue.ToString()) Then
                    If Concepto.InsertarConcepto(Trim(TxtDescripcion.Text), TxtIva.Text, CboImputacion.SelectedValue.ToString(), CboTipo.Text) Then
                        Me.Response.Write("<script languaje='javascript'>window.alert('Se completo el Alta')</script>")
                        ProcLimpiarForm()
                    Else
                        LblError.Visible = True
                        LblError.Text = "Error, no se pudo realizar la insercion"
                    End If
                Else
                    LblError.Visible = True
                    LblError.Text = "Error, el concepto ya existe"
                End If
            Else
                LblError.Visible = True
            End If
        Else
            If Cont = 0 Then
                If Not Concepto.Existe(Trim(TxtDescripcion.Text), CboTipo.SelectedValue.ToString()) Then
                    If Concepto.ModificarConcepto(Int(HFCodigoConcepto.Value), _
                    Trim(TxtDescripcion.Text), _
                    CDbl(TxtIva.Text), Int(CboImputacion.SelectedValue.ToString()), CboTipo.Text) Then
                        Me.Response.Write("<script languaje='javascript'>window.alert('La Modificacion fue Correcta')</script>")
                        ProcLimpiarForm()
                    Else
                        LblError.Visible = True
                        LblError.Text = "Error, no se pudo realizar la insercion"
                    End If

                Else
                    LblError.Visible = True
                    LblError.Text = "Error, el concepto ya existe"
                End If
            Else
                LblError.Visible = True
            End If
        End If
    End Sub
    Sub ProcLlenarGrid()
        Dim TIva As String
        Dim TDesc As String = ""
        Dim CTipo As String
        Dim CImpu As String
        If (TxtIva.Text = "0,00" Or TxtIva.Text = "") Then
            TIva = ""
        Else
            TIva = TxtIva.Text
        End If
        If CboTipo.SelectedValue.ToString() = "-1" Then
            CTipo = ""
        Else
            CTipo = CboTipo.SelectedValue.ToString()
        End If
        If CboImputacion.SelectedValue.ToString() = -1 Then
            CImpu = 0
        Else
            CImpu = CboImputacion.SelectedValue.ToString()
        End If
        If TxtDescripcion.Text <> "" Then
            TDesc = TxtDescripcion.Text
        Else
            TDesc = ""
        End If
        HFCodigoConcepto.Value = ""
        GridConcepto.DataSource = Concepto.ListarTodo(0, Trim(TDesc), TIva, CImpu, CTipo)
        GridConcepto.DataBind()
    End Sub
    Sub LlenarComboIimputacion()
        CboImputacion.DataSource = imputacion.ListarImputacion(0, "", "")
        CboImputacion.DataTextField = "DescripcionImp"
        CboImputacion.DataValueField = "CodigoImputacion"
        CboImputacion.DataBind()
    End Sub
    Sub ProcLimpiarForm()
        TxtDescripcion.Text = ""
        TxtIva.Text = ""
        CboTipo.SelectedIndex = -1
        GridConcepto.SelectedIndex = -1
        CboImputacion.SelectedIndex = -1
         ProcLlenarGrid()
        HFCodigoConcepto.Value = ""
        LblError.Text = ""
        LblError.Visible = False
    End Sub
    Protected Sub LinkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkBuscar.Click
        ProcLlenarGrid()
    End Sub

    Protected Sub GridConcepto_RowCommand(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridConcepto.RowCommand
        If e.CommandName = "Select" Then
            TxtDescripcion.Text = e.CommandSource.parent.parent.FindControl("LblDescripcion").text
            TxtIva.Text = e.CommandSource.Parent.Parent.FindControl("LblIva").text
            HFCodigoConcepto.Value = e.CommandSource.Parent.FindControl("HdConcepto").value
            CboImputacion.SelectedValue = e.CommandSource.parent.parent.FindControl("HFImputacion").value
            If Trim(e.CommandSource.Parent.Parent.FindControl("LblTipo").text()) = "Compra" Then
                CboTipo.Text = "Compra"
            Else
                CboTipo.Text = "Venta"

            End If

        End If
    End Sub
    Protected Sub LinkEliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkEliminar.Click
        LblError.Visible = False
        LblError.Text = ""
        If HFCodigoConcepto.Value <> "" Then
            If TxtDescripcion.Text <> "" Then
                If Concepto.EliminarConcepto(HFCodigoConcepto.Value) Then
                    Me.Response.Write("<script languaje='javascript'>window.alert('La eliminacion se realizo correctamente')</script>")
                    ProcLimpiarForm()

                End If
            Else
                Me.Response.Write("<script languaje='javascript'>window.alert('No se pudo realizar la elimina')</script>")
                ProcLimpiarForm()
            End If
        Else
            LblError.Text = "No selecciono elemento para eliminar!"
            LblError.Visible = True
        End If
    End Sub

    Protected Sub LinkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkCancelar.Click
        ProcLimpiarForm()
    End Sub

End Class